<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div class="modal-body" id="privilege-modal">
	<select class="priviType" onchange="changeTree()"></select>
</div>
<div class="modal-footer">
	<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	<button type="button" id="privilege-submit-btn" class="btn btn-primary">确定</button>
</div>

<script type="text/javascript">
	$(function() {

		var setting = {
		view : {
			dblClickExpand : false
		},
		check : {
		enable : true,
		chkboxType : {
		"Y" : "ps",
		"N" : "s"
		}
		},
		data : {
		simpleData : {
			enable : false
		},
		key : {
		children : "children",
		id : "objId",
		name : "objName"
		}
		}
		};

		var changeTree = function() {
			var v = $('select.priviType').val();
			$('ul.' + v).show();
		};

		$.ajax({
		url : ctxPath + '/privilege/privilege!treeList.action?authType=${authType}&authObjId=${authObjId}',
		success : function(data) {
			var objs = util.strToObj(data);
			for (var i = 0; i < objs.length; i++) {
				var obj = objs[i];
				$('select.priviType').append('<option value="'+obj.typeKey+'">' + obj.typeName + '</option>');
				$('select.priviType').after('<ul  class="ztree '+obj.typeKey+'" id="'+obj.treeId+'"></ul>');
				var zNodes = util.strToObj(obj.datas);
				var zTree = $.fn.zTree.init($('#' + obj.treeId), setting, zNodes);
				if (zTree.getNodes().length > 0) {
					var node = zTree.getNodes()[0];
					zTree.expandNode(node, true, false, false);
					var arr = obj.checkDatas;
					if (arr.length > 0) {
						for (var j = 0; j < arr.length; j++) {
							arr[j] = $.trim(arr[j]);
						}

						var nodes = zTree.getNodesByFilter(function (targetNode) {
							var flag = ($.inArray(targetNode.objId, arr) >= 0);
							return flag;
						}); // 查找节点集合

						for (var k = 0; k < nodes.length; k++) {
							zTree.checkNode(nodes[k], true, false);
						}
					}
				}
			}
			changeTree();
		}
		});



		$("#privilege-submit-btn").click(function() {
			var priviObjs = new Array();
			$('ul.ztree').each(function(i, item) {
				var id = $(item).attr('id');
				var zTree = $.fn.zTree.getZTreeObj(id);
				var nodes = zTree.getCheckedNodes(true);
				var priviObj = new Object();
				var ids = new Array();
				if (nodes.length <= 0) {
					var firstNode = zTree.getNodes()[0];
					priviObj.typeKey = firstNode.priviType;
				} else {
					priviObj.typeKey = nodes[0].priviType;
				}
				for (var j = 0; j < nodes.length; j++) {
					ids[ids.length] = nodes[j].objId;
				}
				priviObj.objIds = ids;
				priviObjs[priviObjs.length] = priviObj;
			});

			var json = JSON.stringify(priviObjs);

			$.ajax({
			url : ctxPath + '/privilege/privilege!reAuth.action?authType=${authType}&authObjId=${authObjId}',
			data : 'priviObjsJson=' + json,
			type : 'post',
			success : function(msg) {
				msg = util.strToObj(msg);
				$("#privilege-modal").parents('.modal').modal('hide');
				if(msg.code=="success")
					dialog.calloutAlert(msg.desc,"","success");
				else
					dialog.calloutAlert(msg.desc,"","error");
			}
			});
		});

	});
</script>